package com.customization.huangwm.proxy.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.customization.huangwm.utils.DBTools;
import com.customization.huangwm.utils.LogTool;
import com.weaverboot.frame.ioc.anno.classAnno.WeaIocReplaceComponent;
import com.weaverboot.frame.ioc.anno.methodAnno.WeaReplaceAfter;
import com.weaverboot.frame.ioc.handler.replace.weaReplaceParam.impl.WeaAfterReplaceParam;
import org.opensaml.xml.signature.J;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2023-02-10
 * @Version 1.0
 * @Description TODO 门户流程元素中标题增加当前状态显示 待办、已办等
 **/
@WeaIocReplaceComponent("newElementWorkflow")
public class ElementForWorkflow extends BaseBean {
    private LogTool logTool = new LogTool("/log/huangwm/proxy/element/workflow",false);

    @WeaReplaceAfter(value = "/api/portal/element/workflow",order = 1)
    public String after1(WeaAfterReplaceParam weaAfterReplaceParam) {
        String data = weaAfterReplaceParam.getData();//这个就是接口执行完的报文
        logTool.writeLog("拦截门户流程中心元素");
        JSONObject dataMap = JSONObject.parseObject(data);
        String addStatus = getPropValue("SW003","addStatus");
        try{
            Map params = weaAfterReplaceParam.getParamMap();
            //得到完整的列表数据
            JSONObject allDatas = dataMap.getJSONObject("data");
            JSONArray dataList = allDatas.getJSONArray("data");
            JSONArray newList = new JSONArray();
            //循环数据 根据requestid获取状态
            RecordSet rs = new RecordSet();
            for(int i=0;i<dataList.size();i++){
                JSONObject json = dataList.getJSONObject(i);

                if(json.containsKey("creater")){
                    json.put("creater",json.getJSONObject("creater"));
                }
                if(json.containsKey("receivedate")){
                    json.put("receivedate",json.getString("receivedate"));
                }
                JSONObject linkinfo = json.getJSONObject("requestname");
                String requestid = linkinfo.getString("requestid");
                String requestname = linkinfo.getString("name");
                String sql = "select requestname,status,currentnodeid,nodename,currentnodetype from workflow_requestbase \n" +
                        "left join workflow_base on workflow_requestbase.workflowid=workflow_base.id\n" +
                        "left join workflow_nodebase on workflow_requestbase.currentnodeid=workflow_nodebase.id\n" +
                        "where workflow_requestbase.requestid="+requestid;
                rs.execute(sql);
                rs.next();
                String nodename = rs.getString("nodename");
                String status = rs.getString("status");
                int currentnodetype = rs.getInt("currentnodetype");
                String newRequestname = "";
                if(currentnodetype==0){
                    newRequestname = requestname+"(状态:创建)";
                }else{
                    newRequestname = requestname+"(状态:"+nodename+")";
                }
                linkinfo.put("oldname",requestname);
                linkinfo.put("newname",newRequestname);
                linkinfo.put("name",newRequestname);

                json.put("requestname",linkinfo);
                newList.add(json);
            }
            allDatas.put("data",newList);//更新数据
            dataMap.put("data",allDatas);
        }catch (Exception e){
            e.printStackTrace();
            JSONObject apidatas = new JSONObject();
            apidatas.put("errmsg","代理接口异常 "+e.getMessage());
            return JSONObject.toJSONString(apidatas);
        }
        logTool.writeLog(JSONObject.toJSONString(dataMap));
        //return JSONObject.toJSONString(dataMap);
        return data;
    }

    @WeaReplaceAfter(value = "/api/portal/element/workflowtab",order = 1)
    public String after2(WeaAfterReplaceParam weaAfterReplaceParam) {
        String data = weaAfterReplaceParam.getData();//这个就是接口执行完的报文
        logTool.writeLog("拦截门户流程中心元素");
        JSONObject dataMap = JSONObject.parseObject(data);
        String addStatus = getPropValue("SW003","addStatus");
        try{
            Map params = weaAfterReplaceParam.getParamMap();
            //得到完整的列表数据
            //JSONObject allDatas = dataMap.getJSONObject("data");
            JSONArray dataList = dataMap.getJSONArray("data");
            JSONArray newList = new JSONArray();
            //循环数据 根据requestid获取状态
            RecordSet rs = new RecordSet();
            for(int i=0;i<dataList.size();i++){
                JSONObject json = dataList.getJSONObject(i);

                if(json.containsKey("creater")){
                    json.put("creater",json.getJSONObject("creater"));
                }
                if(json.containsKey("receivedate")){
                    json.put("receivedate",json.getString("receivedate"));
                }
                JSONObject linkinfo = json.getJSONObject("requestname");
                String requestid = linkinfo.getString("requestid");
                String requestname = linkinfo.getString("name");
                String sql = "select requestname,status,currentnodeid,nodename,currentnodetype from workflow_requestbase \n" +
                        "left join workflow_base on workflow_requestbase.workflowid=workflow_base.id\n" +
                        "left join workflow_nodebase on workflow_requestbase.currentnodeid=workflow_nodebase.id\n" +
                        "where workflow_requestbase.requestid="+requestid;
                rs.execute(sql);
                rs.next();
                String nodename = rs.getString("nodename");
                String status = rs.getString("status");
                int currentnodetype = rs.getInt("currentnodetype");
                String newRequestname = "";
                if(currentnodetype==0){
                    newRequestname = requestname+"(状态:创建)";
                }else{
                    newRequestname = requestname+"(状态:"+nodename+")";
                }
                linkinfo.put("oldname",requestname);
                linkinfo.put("newname",newRequestname);
                linkinfo.put("name",newRequestname);

                json.put("requestname",linkinfo);
                newList.add(json);
            }
            dataMap.put("data",newList);//更新数据
        }catch (Exception e){
            e.printStackTrace();
            JSONObject apidatas = new JSONObject();
            apidatas.put("errmsg","代理接口异常 "+e.getMessage());
            return JSONObject.toJSONString(apidatas);
        }
        logTool.writeLog(JSONObject.toJSONString(dataMap));
        //return JSONObject.toJSONString(dataMap);
        return data;
    }
}
